package edu.ccut.saturn.studio.dialog.informationdialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnInformationNode;

public class SaturnInformationDialog extends TitleAreaDialog {

	private Text idText;
	private SaturnInformationNode saturnInformationNode;
	
	private Text nameText, expressionText, descriptionText;
	
	private Label nameLabel, expressionLabel,loadstart_label, descriptionLabel;
	
	private Combo loadstartcombo;
	
	public SaturnInformationDialog(Shell parentShell, SaturnInformationNode saturnInformationNode){
		super(parentShell);
		this.saturnInformationNode = saturnInformationNode;
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnInformationNode.getName());
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));
		
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		final Label idLabel = new Label(container, SWT.NONE);
		idLabel.setText(Messages.getString("SaturnInformationDialog.ID")); //$NON-NLS-1$

		idText = new Text(container, SWT.READ_ONLY | SWT.BORDER);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.verticalIndent = 10;
		gd_idText.horizontalIndent = 10;
		idText.setLayoutData(gd_idText);
		
		nameLabel = new Label(container, SWT.NONE);
		final GridData gd_nameLabel = new GridData();
		gd_nameLabel.verticalIndent = 10;
		nameLabel.setLayoutData(gd_nameLabel);
		nameLabel.setText(Messages.getString("SaturnInformationDialog.name"));  //$NON-NLS-1$
		
		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_nameText.verticalIndent = 10;
		gd_nameText.horizontalIndent = 10;
		gd_nameText.widthHint = 621;
		nameText.setLayoutData(gd_nameText);
		
		expressionLabel = new Label(container, SWT.NONE);
		final GridData gd_expressionLabel = new GridData();
		gd_expressionLabel.verticalIndent = 10;
		expressionLabel.setLayoutData(gd_expressionLabel);
		expressionLabel.setText(Messages.getString("SaturnInformationDialog.expression"));  //$NON-NLS-1$
		
		expressionText = new Text(container, SWT.BORDER);
		expressionText.setTextLimit(150);
		expressionText.setEnabled(false);
		final GridData gd_expressionText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_expressionText.verticalIndent = 10;
		gd_expressionText.horizontalIndent = 10;
		expressionText.setLayoutData(gd_expressionText);
		if (this.saturnInformationNode.getKey().endsWith(Messages.getString("SaturnInformationDialog.logic"))) { //$NON-NLS-1$
			loadstart_label = new Label(container, SWT.NONE);
			final GridData gd_loadstart_label = new GridData();
			gd_loadstart_label.verticalIndent = 10;
			loadstart_label.setLayoutData(gd_loadstart_label);
			loadstart_label.setText(Messages.getString("SaturnInformationDialog.LoadStart"));		 //$NON-NLS-1$
			
			loadstartcombo = new Combo(container, SWT.NONE);
			loadstartcombo.setItems(new String[] {Messages.getString("SaturnInformationDialog.false"), Messages.getString("SaturnInformationDialog.true")}); //$NON-NLS-1$ //$NON-NLS-2$
			final GridData GridData_startcombo = new GridData(SWT.FILL, SWT.FILL, true, false);
			GridData_startcombo.verticalIndent = 10;
			GridData_startcombo.horizontalIndent = 10;
			loadstartcombo.setLayoutData(GridData_startcombo);
			loadstartcombo.setText(saturnInformationNode.getSelection().trim());
			
		}

		descriptionLabel = new Label(container, SWT.NONE);
		final GridData gd_descriptionLabel = new GridData(SWT.FILL, SWT.TOP, false, true);
		gd_descriptionLabel.verticalIndent = 14;
		descriptionLabel.setLayoutData(gd_descriptionLabel);
		descriptionLabel.setText(Messages.getString("SaturnInformationDialog.description"));  //$NON-NLS-1$
		
		descriptionText = new Text(container, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.FILL, true, false);
		gd_descriptionText.heightHint = 53;
		gd_descriptionText.verticalIndent = 10;
		gd_descriptionText.horizontalIndent = 10;
		descriptionText.setLayoutData(gd_descriptionText);
		
		// 初始化操作
		idText.setText(saturnInformationNode.getId().trim());
		nameText.setText(saturnInformationNode.getName().trim());
		nameText.setSelection(saturnInformationNode.getName().trim().length());
		
		descriptionText.setText(saturnInformationNode.getDescription().trim());
		expressionText.setText(saturnInformationNode.getKey().trim());
		
		
		/**
		 * 名称不可为空
		 * 
		 * */
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){
					setErrorMessage(Messages.getString("SaturnInformationDialog.NameLimit"));  //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null); 
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		return area;
	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			saturnInformationNode.setKey(expressionText.getText().trim());
			if (this.saturnInformationNode.getKey().endsWith(Messages.getString("SaturnInformationDialog.logic"))) { //$NON-NLS-1$
				saturnInformationNode.setSelection(loadstartcombo.getText());
			}
			saturnInformationNode.setDescription(descriptionText.getText().trim());
			saturnInformationNode.setName(nameText.getText().trim());
		}
		super.buttonPressed(buttonId);
	}

	@Override
	protected Point getInitialSize() {
		return new Point(530, 341);
	}
	
	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
}
